<!-- 入住组件的上层组件 -->
<template>
  <div class="SelectTop-container">
    <MongokuanLayer :isMongokuanLayer="isMongokuanLayer" @click="close">
      <div class="main">
        <SelectDormitory
          v-if="showWho === 1"
          @closeMasking="close"
          :dormitoryArr="dormitoryArrRef"
        ></SelectDormitory>
        <SelectFloor
          v-if="showWho === 2"
          @closeMasking="close"
          :FloorArr="FloorArrRef"
        ></SelectFloor>
        <SelectRoom v-if="showWho === 3" @closeMasking="close" :RoomArr="RoomArrRef"></SelectRoom>
        <Selectbednumber
          v-if="showWho === 4"
          @closeMasking="close"
        ></Selectbednumber>
      </div>
    </MongokuanLayer>
    <div class="tilte">
      <Icon type="selectSS"></Icon>
      选择宿舍
    </div>
    <div class="select">
      <div
        class="item"
        @click="selectClick(1, $event)"
        :class="{ disabled: dormitoryArrRef.length === 1 }"
      >
        选择宿舍楼
      </div>
      <div
        class="item"
        :class="{ disabled: !readCheckInfo.dormitory || FloorArrRef.length === 1 }"
        @click="selectClick(2, $event)"
      >
        选择楼层
      </div>
      <div
        class="item"
        :class="{ disabled: !readCheckInfo.floor }"
        @click="selectClick(3, $event)"
      >
        选择房间号
      </div>
      <div
        class="item"
        :class="{ disabled: !readCheckInfo.room }"
        @click="selectClick(4, $event)"
      >
        选择床号
      </div>
    </div>
  </div>
</template>

<script setup>
import Icon from "@/components/Icon/index.vue";
import MongokuanLayer from "@/components/MongokuanLayer/index.vue";
import SelectDormitory from "./SelectDormitory.vue";
import SelectFloor from "./SelectFloor.vue";
import SelectRoom from "./SelectRoom.vue";
import Selectbednumber from "./Selectbednumber.vue";

import { storeToRefs } from "pinia";
import { useCheckInStore } from "@/store/useCheckInStore.js";
import { useUserStore } from "@/store/useUserStore.js";
const { readUserInfo } = storeToRefs(useUserStore());
const store = useCheckInStore();
const { getDormitoryInfo, getFloorInfo, getRoomNoInfo } = store;
const { readCheckInfo } = storeToRefs(store);

import { onMounted, ref, watchEffect } from "vue";
// 蒙层是否显示
const isMongokuanLayer = ref(false);
// 显示哪个信息
const showWho = ref(null);
// 宿舍楼的信息
const dormitoryArrRef = ref([]);
// 楼层信息
const FloorArrRef = ref([]);
// 房间信息
const RoomArrRef = ref([]);
// 点击关闭按钮关闭蒙层
function close() {
  isMongokuanLayer.value = false;
}

onMounted(() => {
  watchEffect(async () => {
    dormitoryArrRef.value = await getDormitoryInfo(readUserInfo.value.classId);
  })
  watchEffect(async () => {
    if(!readCheckInfo.value.dormitory.id){
      return
    }
    FloorArrRef.value = await getFloorInfo({
        dormitoryId: readCheckInfo.value.dormitory.id,
        classId: readUserInfo.value.classId,
      });
  })
  watchEffect(async ()=>{
    if(!readCheckInfo.value.floor.id){
      return
    }
    RoomArrRef.value = await getRoomNoInfo({
        dormitoryId: readCheckInfo.value.dormitory.id,
        floorId: readCheckInfo.value.floor.id,
        classId: readUserInfo.value.classId,
      });
  })
});

// 开启选择
function selectClick(index, e) {
  if (!e.target.classList.contains("disabled")) {
    isMongokuanLayer.value = true;
    showWho.value = index;
  }
}
</script>

<style lang="less" scoped>
@import "@/styles/var.less";
.SelectTop-container {
  width: 100%;
  height: 100%;
  .main {
    margin: 0 auto;
    width: 80%;
    height: 100%;
  }
  .tilte {
    font-size: 25px;
    font-weight: bold;
    margin-left: 10px;
    .icon-container {
      color: @primary;
    }
  }
  .select {
    width: 100%;
    height: calc(100% - 35px);
    background-color: @bakeground;
    backdrop-filter: blur(5px);
    box-shadow: 2px 2px 20px #626262;
    margin-top: 5px;
    border-radius: 5px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .item {
      cursor: pointer;
      height: 40px;
      line-height: 40px;
      position: relative;
      &.disabled {
        color: @gray;
        cursor: no-drop;
        &::before {
          content: "";
          width: 0%;
          height: 3px;
          position: absolute;
          left: 0;
          bottom: 0;
          background: @suspension;
          transition: all 0.5s linear;
        }
        &:hover::before {
          width: 0;
        }
        &:hover {
          color: @gray;
        }
      }
      &::before {
        content: "";
        width: 0%;
        height: 3px;
        position: absolute;
        left: 0;
        bottom: 0;
        background: @suspension;
        transition: all 0.5s linear;
      }
      &:hover::before {
        width: 100%;
      }
      &:hover {
        color: @suspension;
      }
    }
  }
}
</style>